package com.forum.impl.talk;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.common.entity.ApiResult;
import com.common.utils.LocalContext;
import com.common.vo.TalkPostCommentInfoVo;
import com.common.vo.TalkPostsReplyInfoVo;
import com.forum.mapper.forum.ForumUserMapper;
import com.forum.mapper.talk.TalkPostsCommentMapper;
import com.forum.mapper.talk.TalkPostsCommentReplyMapper;
import com.forum.param.TalkPostsCommentQueryParam;
import com.forum.pojo.forum.ForumUser;
import com.forum.pojo.talk.TalkPostsComment;
import com.forum.pojo.talk.TalkPostsCommentReply;
import com.forum.service.talk.TalkPostsCommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Tsing
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TalkPostsCommentServiceImpl extends ServiceImpl<TalkPostsCommentMapper, TalkPostsComment> implements TalkPostsCommentService {
    private final TalkPostsCommentMapper commentMapper;
    private final ForumUserMapper userMapper;
    private final TalkPostsCommentReplyMapper replyMapper;

    @Override
    public ApiResult<String> sendComment(TalkPostsComment comment) {
        Integer currentUserId = LocalContext.getCurrentUserId();
        comment.setPublisherId(currentUserId);
        commentMapper.insert(comment);
        return ApiResult.success("发送成功");

    }

    @Override
    public ApiResult<Map<String, Object>> getCommentList(TalkPostsCommentQueryParam queryParam) {
        //获取评论
        Long page = queryParam.getPage()==1||queryParam.getPage()==0?0:(queryParam.getPage()-1)*10;
        List<TalkPostsComment> commentList = commentMapper.getCommentList(queryParam.getPostsId(), page);
        //获取总量
        Long commentCount = commentMapper.commentCount(queryParam.getPostsId());
        List<TalkPostCommentInfoVo> talkPostCommentInfoVos = commentList.stream().map(comment -> {
            TalkPostCommentInfoVo commentInfoVo = new TalkPostCommentInfoVo();
            BeanUtils.copyProperties(comment,commentInfoVo);
            //获取发布评论的用户昵称和头像
            ForumUser commentUser = userMapper.selectById(comment.getPublisherId());
            commentInfoVo.setNickname(commentUser.getNickname());
            commentInfoVo.setAvatar(commentUser.getAvatar());
            //获取评论的回复
            LambdaQueryWrapper<TalkPostsCommentReply> replyWrapper = new LambdaQueryWrapper<>();
            replyWrapper.eq(TalkPostsCommentReply::getCommentId, comment.getId());
            List<TalkPostsCommentReply> replies = replyMapper.selectList(replyWrapper);
            //获取评论的用户昵称和头像
            List<TalkPostsReplyInfoVo> replyInfoVos = replies.stream().map(reply -> {
                TalkPostsReplyInfoVo replyInfoVo = new TalkPostsReplyInfoVo();
                BeanUtils.copyProperties(reply, replyInfoVo);

                //获取回复的发送者信息
                ForumUser replyUser = userMapper.selectById(reply.getPublisherId());
                replyInfoVo.setNickname(replyUser.getNickname());
                replyInfoVo.setAvatar(replyUser.getAvatar());
                //获取被回复者的信息
                ForumUser parentUser = userMapper.selectById(reply.getParentUserId());
                replyInfoVo.setParentNickname(parentUser.getNickname());
                return replyInfoVo;
            }).collect(Collectors.toList());
            commentInfoVo.setReplyInfoVos(replyInfoVos);
            return commentInfoVo;

        }).collect(Collectors.toList());

        Map<String,Object> map = new HashMap<>(16);
        map.put("total",commentCount);
        map.put("list",talkPostCommentInfoVos);
        return ApiResult.success(map);
    }
}
